﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Xml.Linq;
using System.Text.RegularExpressions;
using System.Text;

namespace PhoneAppProject1
{
    public partial class DefaultPage : PhoneApplicationPage
    {
        public DefaultPage()
        {
            InitializeComponent();
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
			
            WebClient webClientObj = new WebClient();
            webClientObj.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClientObj_DownloadStringCompleted);
            webClientObj.DownloadStringAsync(new Uri("http://rssfeeds.sanook.com/rss/feeds/sanook/game.hit.xml", UriKind.RelativeOrAbsolute));
            //webClientObj.DownloadStringAsync(new Uri("http://www.pingbook.com/rss/news_feed.php", UriKind.RelativeOrAbsolute));
        }


        public string UTF(string str)
        {
            Encoding unicode = Encoding.Unicode;
            Encoding utf8 = Encoding.UTF8;
            byte[] unicodeBytes = unicode.GetBytes(str);
            byte[] utf8Bytes = Encoding.Convert(unicode,
                                            utf8,
                                            unicodeBytes);
            string stringstr = Encoding.UTF8.GetString(utf8Bytes, 0, utf8Bytes.Length);
            Console.WriteLine("UTF Bytes:");
            StringBuilder sb = new StringBuilder();
            foreach (byte b in utf8Bytes)
            {
                sb.Append(b).Append(" : ");
            }
            return stringstr.ToString();
        }
        void webClientObj_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            try
            {
                ListItem.SelectionChanged -= new SelectionChangedEventHandler(ListItem_SelectionChanged);
                myProgress.Visibility = System.Windows.Visibility.Visible;
                myProgress.IsIndeterminate = !myProgress.IsIndeterminate;
                XElement twitterObj = XElement.Parse(e.Result);
                var Objv = from Obj in twitterObj.Descendants("channel")
                           from ObjIn in Obj.Descendants("item")
                           select new XMLPaser
                           {
                               //ImageURL = "http://btgsf1.fsanook.com/css/sanook/imgs/sanook_rss_logo.gif"

                               ImageURL = ObjIn.Element("enclosure").Attribute("url").Value,
                               CreateDate = Convert.ToDateTime(ObjIn.Element("pubDate").Value).ToString("dddd MMMM yyyy"),
                               Message = Regex.Replace(ObjIn.Element("description").Value.Replace("&nbsp;", ""), @"<[^>]*>", String.Empty)
                           };

                ListItem.ItemsSource = Objv;
                myProgress.IsIndeterminate = !myProgress.IsIndeterminate;
                myProgress.Visibility = System.Windows.Visibility.Collapsed;
                ListItem.SelectionChanged += new SelectionChangedEventHandler(ListItem_SelectionChanged);
            }
            catch
            {
                ListItem.SelectionChanged -= new SelectionChangedEventHandler(ListItem_SelectionChanged);

            }
            //throw new NotImplementedException();
        }

        private void ListItem_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            XMLPaser Objv = (XMLPaser)ListItem.SelectedValue;
            MessageBox.Show(Objv.ImageURL);
            //NavigationService.Navigate(new Uri("/Detail_News.xaml", UriKind.RelativeOrAbsolute));
        }
    }
}
